package com.decent.manager.system.dao;

import com.decent.common.entity.manager.SystemMenu;
import com.decent.manager.system.dto.SystemRoleDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 角色菜单sql管理
 *
 * @author wangyan
 **/
@Mapper
public interface SystemRoleMenuDao {
    /**
     * 根据id查询配置的目录
     *
     * @param roleId 角色id
     * @return 结果
     */
    @Select("<script>" +
            "select mm.id, " +
            "       mm.menu_type, " +
            "       mm.router, " +
            "       mm.permission, " +
            "       mm.component, " +
            "       mm.menu_code, " +
            "       mm.router, " +
            "       mm.pid, " +
            "       mm.icon, " +
            "       mm.menu_name, " +
            "       mm.query_route, " +
            "       mm.visibility, " +
            "       mm.redirect, " +
            "       mm.update_time, " +
            "       mm.menu_sort " +
            " from system_menu mm " +
            "<if test =\"roleId != null \">" +
            " inner join system_role_menu rm on rm.menu_id = mm.id and rm.role_id = #{roleId} " +
            "</if>" +
            " where mm.menu_status = 'NORMAL' " +
            " order by mm.menu_sort" +
            "</script>")
    List<SystemMenu> queryRolesMenu(@Param("roleId") Long roleId);

    /**
     * 清空角色绑定的权限
     *
     * @param role 清空信息
     */
    @Delete("delete from system_role_menu where role_id = #{id}")
    void clearRoleMenu(SystemRoleDTO role);

    /**
     * 授权菜单
     *
     * @param role   角色信息
     * @param menuId 菜单id
     */
    @Insert("insert into system_role_menu " +
            "(role_id, menu_id, creator, create_time, last_modifier, update_time) " +
            " values " +
            "(#{role.id}, #{menuId},  #{role.creator}, now(), #{role.creator}, now()) " +
            " on duplicate key update last_modifier = #{role.creator}, update_time = now()")
    void grantMenu(@Param("role") SystemRoleDTO role, @Param("menuId") Long menuId);
}
